package com.example.repository;

import com.example.data.attendance.Attendance;
import com.example.data.attendance.AttendanceDO;
import com.example.mappers.AttendanceRowMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Repository
//?@Service
public class AttendanceRepository {

    @Autowired
    private JdbcTemplate jdbcTemplate;
    //获取所有打卡记录
    public List<Attendance> getAllAttendance(){
        String sql = "SELECT * FROM attendance ORDER BY empId";
        List<Attendance> attendanceList = jdbcTemplate.query(sql, new AttendanceRowMapper());
        return attendanceList;
    }
    //根据empID与attendanceID查询打卡记录，查询规则同approval
    public Attendance getAttendanceByID(Long empID, Long attendanceID){
        String sql = "SELECT * FROM student WHERE empId=? OR attendanceId=?";
        Attendance attendance = jdbcTemplate.queryForObject(sql, new AttendanceRowMapper(), empID,attendanceID);
        return attendance;
    }
    //添加打卡记录
    public Attendance addAttendance(Attendance attendance){
        String sql = "INSERT INTO attendance (attendanceId,empId,clockTime) VALUES(?, ?, ?)";
        jdbcTemplate.update(sql, attendance.getAttendanceID(), attendance.getEmpID(), attendance.getClockTime());
        String sql2 = "SELECT * FROM attendance WHERE attendanceId=? ";
        Attendance attendance2 = jdbcTemplate.queryForObject(sql2, new AttendanceRowMapper(), attendance.getAttendanceID());
        return attendance2;
    }

    //if it may need
    public Attendance updateAttendanceById( Long attendanceID,Attendance attendance){
        String sql = "UPDATE attendance set empId = ?,clockTime = ? WHERE attendanceId=?";
        jdbcTemplate.update(sql, attendance.getEmpID(),attendance.getClockTime(),attendanceID);
        return getAttendanceByID(attendance.getEmpID(),attendanceID);
    }

    //删除打卡记录
    public void deleteAttendance(long attendanceID){
        String sql = "DELETE FROM attendance WHERE attendanceId=?";
        jdbcTemplate.update(sql, attendanceID);
    }
    public List<Attendance> getFakeDO(){
        Attendance attendanceInfo = new AttendanceDO();
        attendanceInfo.setEmpID(11L);
        attendanceInfo.setAttendanceID(22L);
        attendanceInfo.setClockTime(new Date());
        return Arrays.asList(attendanceInfo);
    }
}
